package com.BEASTMODE.libs.Network;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.EventListener;
import android.net.*;
import android.net.wifi.*;
import android.util.Log;

public class Network
{
	private static final String _centralHost = "cioffstix.sytes.net";
	
	private boolean _connected;
	
	private Socket _clientSocket;
	
	private static ArrayList<NetworkListener> _onConnectListeners = null;
	private static ArrayList<NetworkListener> _onDisconnectListeners = null;
	private static ArrayList<NetworkListener> _onMessageReceived = null;
	private static ArrayList<NetworkListener> _onNetworkError = null;

	private static Network _highlander;
	public static void Initialize()
	{
		if (_highlander == null)
		{
			_highlander = new Network();
		}
	}
	
	public static boolean Connect(String HostName, int Port)
	{
		if (_highlander == null || _highlander._connected)
			return false;
		boolean retval = false;
		try
		{
			Log.d("com.BEASTMODE.libs.Network.Network.Connect", "Connecting....");
			_highlander._clientSocket = new Socket(HostName, Port);
			
			retval = true;
		}
		catch(Exception ex)
		{
			Log.e("Network.Connect", "Connecting threw an exception: ", ex);
		}
		return retval;
	}
	
	public static boolean Disconnect()
	{
		return false;
	}
	
	public static boolean Send(String Message)
	{
		return false;
	}
	
	public static boolean Send(String Destination, Short Port, String Message)
	{
		return false;
	}
	
	public static boolean Connected()
	{
		if (_highlander != null)
			return _highlander._connected;
		return false;
	}
	
	public static void addConnectListener(NetworkListener EL)
	{
		if (_onConnectListeners == null)
			_onConnectListeners = new ArrayList<NetworkListener>();
		_onConnectListeners.add(EL);
	}
	public static void removeConnectListener(NetworkListener EL)
	{
		if (_onConnectListeners == null)
			return;
		_onConnectListeners.remove(EL);
	}
	
	public static void addDisconnectListener(NetworkListener EL)
	{
		if (_onDisconnectListeners == null)
			_onDisconnectListeners = new ArrayList<NetworkListener>();
		_onDisconnectListeners.add(EL);
	}
	public static void removeDisconnectListener(NetworkListener EL)
	{
		if (_onDisconnectListeners == null)
			return;
		_onDisconnectListeners.remove(EL);
	}
	
	public static void addMessageRecievedListener(NetworkListener EL)
	{
		if (_onMessageReceived == null)
			_onMessageReceived = new ArrayList<NetworkListener>();
		_onMessageReceived.add(EL);
	}
	public static void removeMessageRecievedListener(NetworkListener EL)
	{
		if (_onMessageReceived == null)
			return;
		_onMessageReceived.remove(EL);
	}
	
	public static void addErrorListener(NetworkListener EL)
	{
		if (_onNetworkError == null)
			_onNetworkError = new ArrayList<NetworkListener>();
		_onNetworkError.add(EL);
	}
	public static void removeErrorListener(NetworkListener EL)
	{
		if (_onNetworkError == null)
			return;
		_onNetworkError.remove(EL);
	}
	
	private Network()
	{
		_connected = false;
	}
}
